<!DOCTYPE html>
<html>
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="utf-8">
  
  <title>变量声明存储类型 | hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="1存储类定义 C++ 程序中变量/函数的范围（可见性）和生命周期。这些说明符放置在它们所修饰的类型之前。 auto声明变量时根据初始化表达式自动推断该变量的类型 auto f=3.14;      //doubleauto s(“hello”);  //const charauto z = new auto(9); // intauto x1 = 5, x2 = 5.0, x3=’r’;//错误，">
<meta property="og:type" content="article">
<meta property="og:title" content="变量声明存储类型">
<meta property="og:url" content="http://yoursite.com/2019/07/02/编程语言/c++/变量声明存储类型/index.html">
<meta property="og:site_name" content="hexo">
<meta property="og:description" content="1存储类定义 C++ 程序中变量/函数的范围（可见性）和生命周期。这些说明符放置在它们所修饰的类型之前。 auto声明变量时根据初始化表达式自动推断该变量的类型 auto f=3.14;      //doubleauto s(“hello”);  //const charauto z = new auto(9); // intauto x1 = 5, x2 = 5.0, x3=’r’;//错误，">
<meta property="og:locale" content="default">
<meta property="og:updated_time" content="2019-07-09T03:11:41.484Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="变量声明存储类型">
<meta name="twitter:description" content="1存储类定义 C++ 程序中变量/函数的范围（可见性）和生命周期。这些说明符放置在它们所修饰的类型之前。 auto声明变量时根据初始化表达式自动推断该变量的类型 auto f=3.14;      //doubleauto s(“hello”);  //const charauto z = new auto(9); // intauto x1 = 5, x2 = 5.0, x3=’r’;//错误，">
  
    <link rel="alternate" href="/atom.xml" title="hexo" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.ico">
  
  
    
  
  <link rel="stylesheet" href="/css/style.css">
  

</head>
</html>
<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    
    <div id="header-inner" class="inner">
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="搜索"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://yoursite.com"></form>
      </div>
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">首页</a>
        
          <a class="main-nav-link" href="/archives">归档</a>
        
          <a class="main-nav-link" href="/categories/高等数学">高等数学</a>
        
          <a class="main-nav-link" href="/categories/线性代数">线性代数</a>
        
          <a class="main-nav-link" href="/categories/数据结构与算法">数据结构与算法</a>
        
          <a class="main-nav-link" href="/categories/English">英语</a>
        
          <a class="main-nav-link" href="/about">关于</a>
        
      </nav>
      
    </div>
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">hexo</a>
      </h1>
      
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-编程语言/c++/变量声明存储类型" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2019/07/02/编程语言/c++/变量声明存储类型/" class="article-date">
  <time datetime="2019-07-01T17:23:17.000Z" itemprop="datePublished">2019-07-02</time>
</a>
    
  <div class="article-category">
    <a class="article-category-link" href="/categories/编程语言/">编程语言</a>►<a class="article-category-link" href="/categories/编程语言/c/">c++</a>
  </div>

  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      变量声明存储类型
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <!-- Table of Contents -->
        
        <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">存储类定义 C++ 程序中变量/函数的范围（可见性）和生命周期。这些说明符放置在它们所修饰的类型之前。</span><br></pre></td></tr></table></figure>
<h2 id="auto"><a href="#auto" class="headerlink" title="auto"></a>auto</h2><p>声明变量时根据初始化表达式自动推断该变量的类型</p>
<p>auto f=3.14;      //double<br>auto s(“hello”);  //const char<em><br>auto z = new auto(9); // int</em><br>auto x1 = 5, x2 = 5.0, x3=’r’;//错误，必须是初始化为同一类型</p>
<h2 id="register"><a href="#register" class="headerlink" title="register"></a>register</h2><p>用于定义存储在寄存器中而不是 RAM 中的局部变量</p>
<p>寄存器只用于需要快速访问的变量,不保证必然在存储器</p>
<h2 id="static"><a href="#static" class="headerlink" title="static"></a>static</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">1. 指示编译器在程序的生命周期内保持局部变量的存在，而不需要在每次它进入和离开作用域时进行创建和销毁。</span><br><span class="line">因此，使用 static 修饰局部变量可以在函数调用之间保持局部变量的值。</span><br><span class="line">2. static 修饰符也可以应用于全局变量。</span><br><span class="line">当 static 修饰全局变量时，会使变量的作用域限制在声明它的文件内。</span><br><span class="line">3. 在 C++ 中，当 static 用在类数据成员上时，会导致仅有一个该成员的副本被类的所有对象共享。</span><br></pre></td></tr></table></figure>
<h2 id="extern"><a href="#extern" class="headerlink" title="extern"></a>extern</h2><p>extern 修饰符通常用于当有两个或多个文件共享相同的全局变量或函数的时候</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">1. extern 存储类用于提供一个全局变量的引用，全局变量对所有的程序文件都是可见的。</span><br><span class="line">2. extern 是用来在另一个文件中声明一个全局变量或函数。</span><br></pre></td></tr></table></figure>
<p>1. </p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2019/07/02/编程语言/c++/变量声明存储类型/" data-id="cjz7utmp7002qxhpe77ufxdi3" class="article-share-link">分享</a>
      
      
      
    </footer>
  </div>
  
    
    
<nav id="article-nav">
  
    <a href="/2019/07/09/编程语言/c++/指针/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">上一篇</strong>
      <div class="article-nav-title">
        
          指针
        
      </div>
    </a>
  
  
    <a href="/2019/07/01/hello-world/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">下一篇</strong>
      <div class="article-nav-title">Hello World</div>
    </a>
  
</nav>

  
</article>
 
     
  

</section>
           
    <aside id="sidebar">
  
    

  
    
    <div class="widget-wrap">
    
      <div class="widget" id="toc-widget-fixed">
      
        <strong class="toc-title">文章目录</strong>
        <div class="toc-widget-list">
              <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#auto"><span class="toc-number">1.</span> <span class="toc-text">auto</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#register"><span class="toc-number">2.</span> <span class="toc-text">register</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#static"><span class="toc-number">3.</span> <span class="toc-text">static</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#extern"><span class="toc-number">4.</span> <span class="toc-text">extern</span></a></li></ol>
          </div>
      </div>
    </div>

  
    

  
    
  
    
  
    

  
    
  
    <!--微信公众号二维码-->


  
</aside>

      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-left">
      &copy; 2014 - 2019 tiger&nbsp;|&nbsp;
      主题 <a href="https://github.com/giscafer/hexo-theme-cafe/" target="_blank">Cafe</a>
    </div>
     <div id="footer-right">
      联系方式&nbsp;|&nbsp;375478250@qq.com
    </div>
  </div>
</footer>
 <script src="/jquery/jquery.min.js"></script>

 <script src='https://unpkg.com/mermaid@8.1.0/dist/mermaid.min.js'></script>
 <script>
 if (window.mermaid) {
	 mermaid.initialize({startOnLoad:true});
 }
</script>

    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">首页</a>
  
    <a href="/archives" class="mobile-nav-link">归档</a>
  
    <a href="/categories/高等数学" class="mobile-nav-link">高等数学</a>
  
    <a href="/categories/线性代数" class="mobile-nav-link">线性代数</a>
  
    <a href="/categories/数据结构与算法" class="mobile-nav-link">数据结构与算法</a>
  
    <a href="/categories/English" class="mobile-nav-link">英语</a>
  
    <a href="/about" class="mobile-nav-link">关于</a>
  
</nav>
    <img class="back-to-top-btn" src="/images/fly-to-top.png"/>
<script>
// Elevator script included on the page, already.
window.onload = function() {
  var elevator = new Elevator({
    selector:'.back-to-top-btn',
    element: document.querySelector('.back-to-top-btn'),
    duration: 1000 // milliseconds
  });
}
</script>
    <!-- author:forvoid begin -->
<!-- author:forvoid end -->


  
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      })
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      })
    </script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML"></script>
  


 <script src="/js/is.js"></script>


  <link rel="stylesheet" href="/fancybox/jquery.fancybox.css">
  <script src="/fancybox/jquery.fancybox.pack.js"></script>


<script src="/js/script.js"></script>
<script src="/js/elevator.js"></script>
  </div>
</body>
</html>
